package com.mzlalal.donate.service.impl;

import com.mzlalal.donate.dao.DonateMapper;
import com.mzlalal.donate.model.Donate;
import com.mzlalal.donate.model.comm.Results;
import com.mzlalal.donate.model.enums.ScanStatusEnum;
import com.mzlalal.donate.service.DonateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @description:
 * @author: Mzlalal
 * @date: 2019/12/28 17:45
 * @version: 1.0
 */
@Slf4j
@Service
public class DonateServiceImpl implements DonateService {

    @Autowired
    DonateMapper donateMapper;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 插入捐赠记录
     *
     * @param donate 捐赠信息
     * @return 1 成功 0失败
     */
    @Override
    public Results insertDonateRecord(String token, Donate donate) {
        // 修改成为已扫描
        donate.setScanStatusEnum(ScanStatusEnum.SCAN_YES);
        String msg = new StringBuilder()
                .append("感谢 ")
                .append(donate.getName())
                .append(" 您捐献的 ")
                .append(donate.getMoneyAmount())
                .append(" 元!").toString();
        try {
            if (donateMapper.insertDonateRecord(donate) > 0) {
                // 更新缓存中的状态
                redisTemplate.opsForValue().set(token, donate);
                return Results.OK(msg);
            }
        } catch (Exception e) {
            log.error("", e);
        }
        return Results.FAIL(msg);
    }
}
